package Control;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * This class holds our data from the A/D-Converter
 * 
 * @author kurmt1@bfh.ch, woodr1@bfh.ch, eichs2@bfh.ch
 * @version 1.0
 */
public class LCPDData {
	private final int NumberOfChannel = 2;

	private LCPDConverterSettings ConvererSettings;
	private ArrayList<Float>[] Samples;
	private boolean[] newData;

	/**
	 * Create all arrays.
	 */
	public LCPDData(LCPDConverterSettings pConverterSettings) {
		Samples = new ArrayList[NumberOfChannel];
		newData=new boolean[NumberOfChannel];
		for (int i=0;i<NumberOfChannel;i++)
		{
			Samples[i]=new ArrayList<Float>();
			newData[i] = false;
		}
		
		ConvererSettings=pConverterSettings;
	}

	/**
	 * return the sampled Data in a List
	 * 
	 * @param ChannelNumber
	 * @return List of the Samples
	 */
	public synchronized ArrayList<Float> getSamples(int ChannelNumber) {
		if (ChannelNumber < NumberOfChannel)
		{
			newData[ChannelNumber]=false;
			return Samples[ChannelNumber];
		}
		else
			return null;
	}

	/**
	 * set the sampled Data in a List
	 * 
	 * @param ChannelNumber
	 *            
	 * @param value List of the Samples
	 *            
	 */
	public synchronized  void setSamples(int ChannelNumber, ArrayList<Float> value) {
		if (ChannelNumber < NumberOfChannel)
		{
			if (newData[ChannelNumber]==false||
					(ConvererSettings.getDisplayTime()*ConvererSettings.getSampleRate())<=Samples[ChannelNumber].size())
			{
				Samples[ChannelNumber].clear();
				//System.out.println("Samples Cleared");
			}
			newData[ChannelNumber]=true;
			Samples[ChannelNumber].addAll(value);		
		}
		else
			Samples[ChannelNumber] = null;

	}

	/**
	 * return wether new data read (until the first read was made)
	 * 
	 * @param ChannelNumber
	 * @return true or false
	 */
	public synchronized  boolean getNewData(int Channel) {
		return newData[Channel];
	}

	/**
	 * set whether new data was read should not be used, setSamples should
	 * automatic update this flag
	 * 
	 * @param ChannelNumber
	 * @param value true or false
	 */
	public synchronized void setNewData(int Channel,boolean value) {
			newData[Channel] = value;
	}
}
